﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyBanHang.QuerySQL;

namespace QuanLyBanHang
{
    public partial class frKhachhang : Office2007Form
    {
        ClassKhachhang kh = new ClassKhachhang();
        string makh;
        int dong;
        bool ts, ktra;
        public frKhachhang()
        {
            InitializeComponent();
        }
        //
        public void Xulybutton(bool b)
        {
            this.btnthem.Enabled = btnsua.Enabled = b;
            btnLuu.Enabled = btnhuy.Enabled = panel_txt.Enabled = !b;
        }

        //
        private void frKhachhang_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnLuu;
            dtgv_KH.DataSource = kh.ShowKH();
            Xulybutton(true);
        }

        private void txttktenv_TextChanged(object sender, EventArgs e)
        {
            dtgv_KH.DataSource = kh.ShowTKH(txttktenkh.Text);
        }

        private void txttkmanv_TextChanged(object sender, EventArgs e)
        {
            dtgv_KH.DataSource = kh.ShowMAKH(txttkmakh.Text);
        }

        private void dtgv_KH_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            this.txtmakh.Text = makh = dtgv_KH.Rows[dong].Cells[1].Value.ToString();
            this.txttenkh.Text = dtgv_KH.Rows[dong].Cells[2].Value.ToString();
            this.txtdiachi.Text = dtgv_KH.Rows[dong].Cells[3].Value.ToString();
            this.txtsdt.Text = dtgv_KH.Rows[dong].Cells[4].Value.ToString();
        }

        private void dtgv_KH_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_KH.RowCount; i++)
                {
                    dtgv_KH.Rows[i].Cells[0].Value = i + 1;
                }
            }
            catch { }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            Xulybutton(false);
            txtmakh.Enabled = true;
            ts = true;
            txtmakh.Text = "kh" + (dtgv_KH.RowCount + 1).ToString();
            txttenkh.Clear();
            txtdiachi.Clear();
            txtsdt.Clear();
            this.AcceptButton = btnLuu;
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            ts = false;
            Xulybutton(false);
            txtmakh.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtmakh.TextLength == 0)
            {
                lbthongbao.Text = "* Hãy nhập vào mã khách hàng...";
                txtmakh.Focus();
            }
            else {
                if (txttenkh.TextLength == 0)
                {
                    lbthongbao.Text = "* Hãy nhập vào tên khách hàng...";
                    txttenkh.Focus();
                }
                else {
                    if (ts)
                    {
                        if (!ktra)
                        {
                            kh.ThemKH(txtmakh.Text,txttenkh.Text,txtdiachi.Text,txtsdt.Text);
                            frKhachhang_Load(sender, e);
                            lbthongbao.Text = "*";
                            Xulybutton(true);
                        }
                        else { MessageBox.Show("Mã nhân viên đã tồn tại !!!"); }
                    }
                    else
                    {
                        kh.SuaKH(makh,txtmakh.Text,txttenkh.Text,txtdiachi.Text,txtsdt.Text);
                        frKhachhang_Load(sender, e);
                        lbthongbao.Text = "*";
                        Xulybutton(true);
                    }
                }
            }
        }

        private void txtmakh_TextChanged(object sender, EventArgs e)
        {
            ktra = false;
            foreach (DataRow row in kh.ShowKH().Rows)
            {
                if (row["MAKH"].ToString().Equals(txtmakh.Text))
                {
                    ktra = true;
                }
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            frKhachhang_Load(sender, e);
            Xulybutton(true);
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnthem.Enabled)
            {
                DialogResult kq = MessageBox.Show("Bạn có chắc muốn xóa Khách hàng " + txttenkh.Text + "  ? ", "Thông báo", MessageBoxButtons.YesNo);
                if (kq == DialogResult.Yes)
                {

                    kh.XoaKH(makh);
                    frKhachhang_Load(sender, e);
                }
            }
            else
            {
                MessageBox.Show("Hãy thực hiện lưu dữ liệu !!!");
            }
        }

        private void txtsdt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    }
}
